package de.velvettear.squirrel.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.UUID;
import java.util.Vector;

import android.os.Handler;
import android.util.Log;
import de.velvettear.squirrel.kernel.File;
import de.velvettear.squirrel.kernel.Statics;

public class FileHelper {

	public static File copy(File m_cOriginal) {
		return copy(m_cOriginal, null, false);
	}
		
	public static File copy(File m_cOriginal, String m_sFileName, boolean override) {
		File m_cCopy = null;
		Log.i("copy()", "BEGIN");
		
		try {
			if (m_sFileName == null) {
				m_cCopy = new File(generateTmpFilePath(m_cOriginal));				
			} else {
				String filePath = generateFilePath(m_cOriginal, m_sFileName);
				java.io.File tmpFile = new java.io.File(filePath);
				
				if (tmpFile.exists()) {
					if (override) {
						tmpFile.delete();
					}
				}
				
				m_cCopy = new File(filePath);
			}			
					
			FileInputStream input = new FileInputStream(m_cOriginal.m_cFile);
			FileOutputStream output = new FileOutputStream(m_cCopy.m_cFile);
			
			byte[] buffer = new byte[1024];
			int singleByte;
			while((singleByte = input.read(buffer)) != -1) {
				output.write(buffer, 0, singleByte);
			}
			input.close();
			output.flush();
			output.close();
			Log.i("copy()", "FILE COPIED " + m_cCopy.m_sFullPath);
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("copy()", e.getMessage());
			}
		}
		
		Log.i("copy()", "END");		
		return m_cCopy;
	}
	
	public static String generateFilePath(File m_cOriginal, String m_sFileName) {
		Log.i("generateFilePath()", "BEGIN");		
		String m_sTmp = m_cOriginal.m_sPath;
		m_sTmp += m_sFileName + getExtension(m_cOriginal);		
		Log.i("generateFilePath()", "PATH " + m_sTmp);	
		
		Log.i("generateFilePath()", "END");		
		return m_sTmp;
	}
	
	public static String generateTmpFilePath(File m_cOriginal) {
		Log.i("generateTmpFilePath()", "BEGIN");		
		String m_sTmp = m_cOriginal.m_sPath;
		m_sTmp += UUID.randomUUID() + getExtension(m_cOriginal);		
		Log.i("generateTmpFilePath()", "PATH " + m_sTmp);	
		
		Log.i("generateTmpFilePath()", "END");		
		return m_sTmp;
	}
	
	public static String getExtension(File m_cFile) {
		Log.i("getExtension()", "BEGIN");
		String extension = "";
		try {
			extension = m_cFile.m_sFullPath.substring(m_cFile.m_sFullPath.lastIndexOf("."), m_cFile.m_sFullPath.length()).toLowerCase();
			Log.i("getExtension()", "EXTENSION " + extension);
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("getExtension()", e.getMessage());
			}
			Log.i("getExtension()", "ERROR GETTING EXTENSION");
		}		
		
		Log.i("getExtension()", "END");
		return extension;
	}
	
	public static String getSizeFormatted(File m_cOriginal) {
		Log.i("getSize()", "BEGIN");
		String formattedSize = " KB";
		float size = getSize(m_cOriginal);
		size = size / 1024;
		if (size > 1024) {
			size = size / 1024;
			formattedSize = " MB";
		}
		if (size > 1024) {
			size = size / 1024;
			formattedSize = " GB";
		}	
		
		DecimalFormat formatter = new DecimalFormat("0.00");
		formattedSize = formatter.format(size) + formattedSize;		
		Log.i("getSize()", "SIZE " + formattedSize);
		
		Log.i("getSize()", "END");
		return formattedSize;
	}
	
	public static long getSize(File m_cOriginal) {
		Log.i("getSize()", "BEGIN");
		long size = m_cOriginal.m_cFile.length();
		Log.i("getSize()", "SIZE " + size);
		
		Log.i("getSize()", "END");
		return size;
	}
	
	public static void createFolders() {
		Log.i("createFolders()", "BEGIN");
						
		try {					
			Vector<String> directories = new Vector<String>();
			directories.add(Statics.PATH_HOME);		
			directories.add(Statics.PATH_PICTURES);
			for (String dir : directories) {
				try {
					java.io.File directory = new java.io.File(dir);
					if (!directory.exists()) {
						directory.mkdirs();
						Log.i("createFolders()", "FOLDER " + dir + " CREATED");
					} else {
						Log.i("createFolders()", "FOLDER " + dir + " EXISTING");
					}
				} catch (Exception ex) {
					if (ex.getMessage() != null) {
						Log.e("createFolders()", ex.getMessage());
					}
				}
			}	
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("createFolders()", e.getMessage());
			}
		}
		
		Log.i("createFolders()", "END");
	}
	
}
